package com.ht.module.common.service.impl;

import com.ht.abnormal.HtException;
import com.ht.constant.DictConstant;
import com.ht.module.common.entity.CommonFile;
import com.ht.module.common.service.ICommonFileService;
import com.ht.module.common.service.ICommonService;
import com.ht.util.CommMethod;
import com.ht.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @ProjectName: ht_screen
 * @ClassName: CommonServiceImpl
 * @Author: hejialun
 * @Description: 公共类实现类
 * @Date: 2021/8/11 10:14
 */
@Service
public class CommonServiceImpl implements ICommonService {
    //注入上传路径
    @Value("${upload-path}")
    private String uploadPath;
    @Autowired
    private ICommonFileService iCommonFileService;

    @Override
    public String uploadFile(MultipartFile file) throws IOException {
        if(file==null){
            throw new HtException("请选择上传文件!");
        }
        //创建文件夹
        File folder = new File(uploadPath);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        //定义文件新的名字
        String newFileName= UUID.randomUUID().toString() + file.getOriginalFilename();
        String newFilePath = uploadPath + newFileName;
        //生成文件
        file.transferTo(new File(newFilePath));
        return newFileName;
    }

    @Override
    public Boolean delFile(String path) {
        try {
            new File(uploadPath+path).delete();
        }catch (Exception e){
            return false;
        }
        return true;
    }
}